#ifndef HIXL_EVENT_LOOP_H
#define HIXL_EVENT_LOOP_H

#include "hccl_socket.h"
#include "hccl_network.h"       // 提供 HcclEpollEvent / SocketEventInfo / RA_EPOLL* 等
#include "hccl_api.h"
#include <map>
#include <mutex>
#include <atomic>
#include <thread>

namespace hixl {
    class HixlCsClient;

    class HixlEventLoop {
    public:
        static HixlEventLoop &GetInstance() {
            static HixlEventLoop instance;
            return instance;
        }

        HixlEventLoop(const HixlEventLoop &) = delete;

        HixlEventLoop &operator=(const HixlEventLoop &) = delete;

        // 注册 / 删除某个 fd 对应的 client
        hccl::HcclResult AddClient(hccl::FdHandle fd, HixlCsClient *client);

        hccl::HcclResult DelClient(hccl::FdHandle fd);

    private:
        HixlEventLoop();

        ~HixlEventLoop();

        void Run(int waitTimeoutMs);

        std::atomic<bool> running_{false};
        std::thread loopThread_;
        s32 eventHandle_{-1}; // epoll event handle

        std::mutex mapMutex_;
        std::map<hccl::FdHandle, HixlCsClient *> clients_;
    };
} // namespace hixl

#endif // HIXL_EVENT_LOOP_H
